package wiki;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.Root;

/**
 * 
 * @author mona.chandrapal
 */
public class GetIssueDetail extends HttpServlet {

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		String issueid = request.getParameter("issueid");

		Root root = new Root();
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql1 = "";
		int nissueid;
		String sql2 = "";
		ResultSet rs2 = null;
		PreparedStatement pst2 = null;
		
		String sql3 = "";
		ResultSet rs3 = null;
		PreparedStatement pst3 = null;

		JSONObject issue, issuedetails;
		issuedetails = new JSONObject();

		JSONObject comments;
		JSONArray commentlist;
		
		JSONObject attachment;
		JSONArray attachmentlist;
		try {

			if ((issueid != null) && (issueid.length() > 0)) {

				nissueid = Integer.parseInt(issueid);
				root.initConnection();

				JSONObject issues = new JSONObject();
				if (issueid.equalsIgnoreCase("0")) {
					sql1 = "Select id, issuetitle, issuedetails, module, category, subcategory, bugid, version, createdby, createdon from issue where deleteflag=0 order by id desc limit 3 ";
					pst = root.con.prepareStatement(sql1);
					rs = pst.executeQuery();
					JSONArray issuelist = new JSONArray();
					while (rs.next()) {
						issue = new JSONObject();
						issue.put("id", rs.getString("id"));
						issue.put("issuetitle", rs.getString("issuetitle"));
						issue.put("issuedetails", rs.getString("issuedetails"));
						issue.put("module", rs.getString("module"));
						issue.put("category", rs.getString("category"));
						issue.put("subcategory", rs.getString("subcategory"));
						issue.put("bugid", rs.getString("bugid"));
						issue.put("version", rs.getString("version"));
						issue.put("createdby", rs.getString("createdby"));
						issue.put("createdon", rs.getString("createdon"));

						issuelist.add(issue);
					}
					// out.println("{\"issuedetails\": [" + issuelist.toString()
					// + "]}");
					issues.put("issuedetails", issuelist);
					out.println(issues.toString());

				} else {
					sql1 = "Select issuetitle, issuedetails, module, category, subcategory, bugid, version, issueidforuser from issue where id = ? and deleteflag =0 ";
					pst = root.con.prepareStatement(sql1);
					pst.setInt(1, nissueid);
					issue = new JSONObject();
					// System.out.println(pst);
					rs = pst.executeQuery();
					rs.next();

					String title = rs.getString("issuetitle");
					String details = rs.getString("issuedetails");

					// System.out.println("Issuedetails: " + details);

					issue.put("issuetitle", title);
					issue.put("issuedetails", details);
					issue.put("module", rs.getString("module"));
					issue.put("category", rs.getString("category"));
					issue.put("subcategory", rs.getString("subcategory"));
					issue.put("bugid", rs.getString("bugid"));
					issue.put("version", rs.getString("version"));
					issue.put("issueidforuser", rs.getString("Issueidforuser"));
					

					// Read comments
					sql2 = "Select id, comment, username,  date from issuedetails where issueid = ? and deleteflag =0 order by id desc";
					pst2 = root.con.prepareStatement(sql2);
					pst2.setInt(1, nissueid);

					commentlist = new JSONArray();
					rs2 = pst2.executeQuery();
					while (rs2.next()) {

						comments = new JSONObject();
						
						//String  viewcomment = rs2.getString("comment");
						//viewcomment=viewcomment.replaceAll("\\n", "<br/>");
						// issuedetails = issuedetails.replaceAll("\\n", "<br>");
						
						comments.put("id",rs2.getString("id") );
						comments.put("comment",rs2.getString("comment") );
						comments.put("date", rs2.getString("date"));
						comments.put("username", rs2.getString("username"));
						
						commentlist.add(comments);

					}

					issue.put("comments", commentlist);

					
					
					// Read attachments
					sql3 = "Select id, filename from attachedfile where issueid = ? and deleteflag =0";
					pst3 = root.con.prepareStatement(sql3);
					pst3.setInt(1, nissueid);

					attachmentlist = new JSONArray();
					rs3 = pst3.executeQuery();
					while (rs3.next()) {

						attachment = new JSONObject();
						
						//String  viewcomment = rs2.getString("comment");
						//viewcomment=viewcomment.replaceAll("\\n", "<br/>");
						// issuedetails = issuedetails.replaceAll("\\n", "<br>");
						
						attachment.put("id",rs3.getString("id") );
						attachment.put("filename",rs3.getString("filename") );
						
						
						attachmentlist.add(attachment);

					}

					issue.put("attachment", attachmentlist);

					
					
					
					
					
					
					
					out.println("{\"issuedetails\": [" + issue.toString()
							+ "]}");

				}
			}

		} catch (Exception e) {
			System.out.println("Exception getting issue: " + e.toString());
		}finally{
			try {
				rs.close();
				pst.close();
				rs2.close();
				pst2.close();
				root.closeConnection();
			} catch (Exception e) {
				
			}
			
		}

	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
